package cn.itheima.service;

import cn.itheima.dao.UserDao;
import cn.itheima.domain.Section;
import cn.itheima.domain.User;
import cn.itheima.domain.UserQueryVo;
import cn.itheima.utils.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: cuisensen
 * data: 2018/6/26.
 * time: 0:49.
 */
public class UserServiceImp implements UserService {
    @Override
    public User login(String uname) {
        SqlSession session = SqlSessionFactoryUtils.getSession();
        UserDao ud=session.getMapper(UserDao.class);
        User user=null;
        try {
            user=ud.findUserByName(uname);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
        if(user==null){
            return null;
        }
        SectionService ss=new SectionServiceImp();
        user.setSection(ss.findSectionBySid(user.getSid()));

        if(user.getRoot()>2&&!user.getSection().getSname().equals("人事部")){
            return null;
        }
        return user;
    }

    @Override
    public boolean register(User user) {
        SqlSession session = SqlSessionFactoryUtils.getSession();
        UserDao ud=session.getMapper(UserDao.class);
        SectionService ss=new SectionServiceImp();
        Section section=new Section();
        section.setSname("BOSS");
        section.setUid(user.getCode());
        int sid=ss.insertSection(section);
        section.setSid(sid);
        user.setSid(sid);
        user.setSection(section);
        try {
            ud.insertUser(user);
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }

        session.commit();

        return true;
    }

    @Override
    public boolean isExist(String uname) {
        SqlSession session = SqlSessionFactoryUtils.getSession();
        UserDao ud=session.getMapper(UserDao.class);
        User user=null;
        try {
            user=ud.findUserByName(uname);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user==null?false:true;
    }

    @Override
    public List<User> findUserByTerm(UserQueryVo query) {
        SqlSession session = SqlSessionFactoryUtils.getSession();
        UserDao mapper = session.getMapper(UserDao.class);

        List<User> userByTerm = mapper.findUserByTerm(query);
        List<User> users=new ArrayList<User>();
        for(User u : userByTerm){
            if(u.getSection().getSname()!=null){
                users.add(u);
            }
        }
        userByTerm.clear();
        return users;
    }

    @Override
    public void insertUser(User user) {
        SqlSession session = SqlSessionFactoryUtils.getSession();
        UserDao ud=session.getMapper(UserDao.class);
        try {
            ud.insertUser(user);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        session.commit();
    }

    @Override
    public User findUserById(String uid) {
        SqlSession session = SqlSessionFactoryUtils.getSession();
        UserDao ud=session.getMapper(UserDao.class);
        User user = ud.findUserById(uid);
        return user;
    }

    @Override
    public void updateUser(User user) {
        SqlSession session = SqlSessionFactoryUtils.getSession();
        UserDao ud=session.getMapper(UserDao.class);
        ud.updateUser(user);
        session.commit();
    }

}
